/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "disc_server_proxy_standard.h"

#include "ipc_skeleton.h"
#include "disc_log.h"
#include "message_parcel.h"
#include "softbus_errcode.h"
#include "softbus_server_ipc_interface_code.h"
#include "softbus_service_discovery.h"

namespace OHOS {
sptr<IRemoteObject> g_discRemoteProxy = nullptr;
static uint32_t g_getSystemAbilityId = 2;
const std::u16string SAMANAGER_INTERFACE_TOKEN = u"ohos.samgr.accessToken";
static sptr<IRemoteObject> GetSystemAbility()
{
    MessageParcel data;

    if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
        return nullptr;
    }

    data.WriteInt32(SOFTBUS_SERVER_SA_ID_INNER);
    MessageParcel reply;
    MessageOption option;
    sptr<IRemoteObject> samgr = IPCSkeleton::GetContextObject();
    DISC_CHECK_AND_RETURN_RET_LOGE(samgr != nullptr, nullptr, DISC_SDK, "Get samgr failed");

    int32_t err = samgr->SendRequest(g_getSystemAbilityId, data, reply, option);
    DISC_CHECK_AND_RETURN_RET_LOGE(err == 0, nullptr, DISC_SDK, "GetSystemAbility failed=%{public}d", err);

    return reply.ReadRemoteObject();
}

int32_t DiscServerProxy::DiscServerProxyStandardInit(void)
{
    if (g_discRemoteProxy != nullptr) {
        DISC_LOGE(DISC_SDK, "init success");
        return SOFTBUS_OK;
    }
    g_discRemoteProxy = GetSystemAbility();
    if (g_discRemoteProxy == nullptr) {
        DISC_LOGE(DISC_SDK, "get system ability fail");
        return SOFTBUS_ERR;
    }
    return SOFTBUS_OK;
}

void DiscServerProxy::DiscServerProxyStandardDeInit(void)
{
    g_discRemoteProxy.clear();
}

int32_t DiscServerProxy::SoftbusRegisterService(const char *clientPkgName, const sptr<IRemoteObject>& object)
{
    (void)clientPkgName;
    (void)object;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::CreateSessionServer(const char *pkgName, const char *sessionName)
{
    (void)pkgName;
    (void)sessionName;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::RemoveSessionServer(const char *pkgName, const char *sessionName)
{
    (void)pkgName;
    (void)sessionName;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::OpenSession(const SessionParam *param, TransInfo *info)
{
    (void)param;
    (void)info;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::OpenAuthSession(const char *sessionName, const ConnectionAddr *addrInfo)
{
    (void)sessionName;
    (void)addrInfo;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::NotifyAuthSuccess(int32_t channelId, int32_t channelType)
{
    (void)channelId;
    (void)channelType;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::ReleaseResources(int32_t channelId)
{
    (void)channelId;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::CloseChannel(const char *sessionName, int32_t channelId, int32_t channelType)
{
    (void)sessionName;
    (void)channelId;
    (void)channelType;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::CloseChannelWithStatistics(int32_t channelId, int32_t channelType, uint64_t laneId,
    const void *dataInfo, uint32_t len)
{
    (void)channelId;
    (void)channelType;
    (void)laneId;
    (void)dataInfo;
    (void)len;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::SendMessage(int32_t channelId, int32_t channelType, const void *data,
    uint32_t len, int32_t msgType)
{
    (void)channelId;
    (void)channelType;
    (void)data;
    (void)len;
    (void)msgType;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::QosReport(int32_t channelId, int32_t chanType, int32_t appType, int32_t quality)
{
    (void)channelId;
    (void)chanType;
    (void)appType;
    (void)quality;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::StreamStats(int32_t channelId, int32_t channelType, const StreamSendStats *data)
{
    (void)channelId;
    (void)channelType;
    (void)data;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::RippleStats(int32_t channelId, int32_t channelType, const TrafficStats *data)
{
    (void)channelId;
    (void)channelType;
    (void)data;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::JoinLNN(const char *pkgName, void *addr, uint32_t addrTypeLen)
{
    (void)pkgName;
    (void)addr;
    (void)addrTypeLen;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::LeaveLNN(const char *pkgName, const char *networkId)
{
    (void)pkgName;
    (void)networkId;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::GetAllOnlineNodeInfo(const char *pkgName, void **info, uint32_t infoTypeLen,
    int32_t *infoNum)
{
    (void)pkgName;
    (void)info;
    (void)infoTypeLen;
    (void)infoNum;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::GetLocalDeviceInfo(const char *pkgName, void *info, uint32_t infoTypeLen)
{
    (void)pkgName;
    (void)info;
    (void)infoTypeLen;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::GetNodeKeyInfo(const char *pkgName, const char *networkId, int32_t key,
    unsigned char *buf, uint32_t len)
{
    (void)pkgName;
    (void)networkId;
    (void)key;
    (void)buf;
    (void)len;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::SetNodeDataChangeFlag(const char *pkgName, const char *networkId, uint16_t dataChangeFlag)
{
    (void)pkgName;
    (void)networkId;
    (void)dataChangeFlag;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::RegDataLevelChangeCb(const char *pkgName)
{
    (void)pkgName;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::UnregDataLevelChangeCb(const char *pkgName)
{
    (void)pkgName;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::SetDataLevel(const DataLevel *dataLevel)
{
    (void)dataLevel;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::StartTimeSync(const char *pkgName, const char *targetNetworkId, int32_t accuracy,
    int32_t period)
{
    (void)pkgName;
    (void)targetNetworkId;
    (void)accuracy;
    (void)period;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::StopTimeSync(const char *pkgName, const char *targetNetworkId)
{
    (void)pkgName;
    (void)targetNetworkId;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::PublishLNN(const char *pkgName, const PublishInfo *info)
{
    (void)pkgName;
    (void)info;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::StopPublishLNN(const char *pkgName, int32_t publishId)
{
    (void)pkgName;
    (void)publishId;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::RefreshLNN(const char *pkgName, const SubscribeInfo *info)
{
    (void)pkgName;
    (void)info;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::StopRefreshLNN(const char *pkgName, int32_t refreshId)
{
    (void)pkgName;
    (void)refreshId;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::ActiveMetaNode(const MetaNodeConfigInfo *info, char *metaNodeId)
{
    (void)info;
    (void)metaNodeId;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::DeactiveMetaNode(const char *metaNodeId)
{
    (void)metaNodeId;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::GetAllMetaNodeInfo(MetaNodeInfo *infos, int32_t *infoNum)
{
    (void)infos;
    (void)infoNum;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::ShiftLNNGear(const char *pkgName, const char *callerId, const char *targetNetworkId,
    const GearMode *mode)
{
    (void)pkgName;
    (void)callerId;
    (void)targetNetworkId;
    (void)mode;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::SyncTrustedRelationShip(const char *pkgName, const char *msg, uint32_t msgLen)
{
    (void)pkgName;
    (void)msg;
    (void)msgLen;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::GetBusCenterExObj(sptr<IRemoteObject> &object)
{
    (void)object;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::EvaluateQos(const char *peerNetworkId, TransDataType dataType, const QosTV *qos,
    uint32_t qosCount)
{
    (void)peerNetworkId;
    (void)dataType;
    (void)qos;
    (void)qosCount;
    return SOFTBUS_OK;
}

int32_t DiscServerProxy::RegisterService(const ServiceInfo *serviceInfo)
{
    DISC_CHECK_AND_RETURN_RET_LOGE(serviceInfo != nullptr, SOFTBUS_INVALID_PARAM, DISC_SDK, "invalid service info");
    sptr<IRemoteObject> remote = GetSystemAbility();
    DISC_CHECK_AND_RETURN_RET_LOGE(remote != nullptr, SOFTBUS_IPC_ERR, DISC_SDK, "remote is nullptr");

    MessageParcel data;
    DISC_CHECK_AND_RETURN_RET_LOGE(data.WriteInterfaceToken(GetDescriptor()), SOFTBUS_IPC_ERR, DISC_SDK, 
        "write interfaceToken failed");
    MessageParcel reply;
    MessageOption option;
    if (remote->SendRequest(SERVER_REGISTER_SERVICE, data, reply, option) != 0) {
        DISC_LOGE(DISC_SDK, "send request failed");
        return SOFTBUS_IPC_ERR;
    }
    int32_t serverRet;
    if (!reply.ReadInt32(serverRet)) {
        DISC_LOGE(DISC_SDK, "read serverRet failed");
        return SOFTBUS_IPC_ERR;
    }
    return serverRet;
}

int32_t DiscServerProxy::UnRegisterService(const char *serviceId)
{
    DISC_CHECK_AND_RETURN_RET_LOGE(serviceId != nullptr, SOFTBUS_INVALID_PARAM, DISC_SDK, "invalid service id");
    sptr<IRemoteObject> remote = GetSystemAbility();
    DISC_CHECK_AND_RETURN_RET_LOGE(remote != nullptr, SOFTBUS_IPC_ERR, DISC_SDK, "remote is nullptr");

    MessageParcel data;
    DISC_CHECK_AND_RETURN_RET_LOGE(data.WriteInterfaceToken(GetDescriptor()), SOFTBUS_IPC_ERR, DISC_SDK, 
        "write interfaceToken failed");
    MessageParcel reply;
    MessageOption option;
    if (remote->SendRequest(SERVER_UNREGISTER_SERVICE, data, reply, option) != 0) {
        DISC_LOGE(DISC_SDK, "send request failed");
        return SOFTBUS_IPC_ERR;
    }
    int32_t serverRet;
    if (!reply.ReadInt32(serverRet)) {
        DISC_LOGE(DISC_SDK, "read serverRet failed");
        return SOFTBUS_IPC_ERR;
    }
    return serverRet;
}

int32_t DiscServerProxy::UpdateService(const ServiceInfo *serviceInfo)
{
    DISC_CHECK_AND_RETURN_RET_LOGE(serviceInfo != nullptr, SOFTBUS_INVALID_PARAM, DISC_SDK, "invalid service info");
    sptr<IRemoteObject> remote = GetSystemAbility();
    DISC_CHECK_AND_RETURN_RET_LOGE(remote != nullptr, SOFTBUS_IPC_ERR, DISC_SDK, "remote is nullptr");

    MessageParcel data;
    DISC_CHECK_AND_RETURN_RET_LOGE(data.WriteInterfaceToken(GetDescriptor()), SOFTBUS_IPC_ERR, DISC_SDK, 
        "write interfaceToken failed");
    MessageParcel reply;
    MessageOption option;
    if (remote->SendRequest(SERVER_UPDATE_SERVICE, data, reply, option) != 0) {
        DISC_LOGE(DISC_SDK, "send request failed");
        return SOFTBUS_IPC_ERR;
    }
    int32_t serverRet;
    if (!reply.ReadInt32(serverRet)) {
        DISC_LOGE(DISC_SDK, "read serverRet failed");
        return SOFTBUS_IPC_ERR;
    }
    return serverRet;
}
} // namespace OHOS
